//
//  LocalMapViewController.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/6/28.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "LocalMapViewController.h"

@interface LocalMapViewController ()<QMapViewDelegate>
{
    NSString *_latitude;
    NSString *_longitude;
}
@property (nonatomic,strong) QPointAnnotation *point;
@end

@implementation LocalMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)initView {
    
    CGFloat windowWidth = self.view.bounds.size.width;
    CGFloat windowHeight = self.view.bounds.size.height;
    _mapView = [[QMapView alloc] initWithFrame:CGRectMake(0,
                                                          64,
                                                          windowWidth,
                                                          windowHeight- 64)];
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    _mapView.showsUserLocation = YES;
    
    _mapView.userTrackingMode = QUserTrackingModeFollow;
    
    _mapView.showsScale = YES;
    
    [_mapView removeAnnotations:_mapView.annotations];
  
}

-(void)mapView:(QMapView *)mapView didUpdateUserLocation:(QUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
    NSLog(@"coordinate:%f,%f, heading:%f", userLocation.coordinate.latitude, userLocation.coordinate.longitude, userLocation.heading.trueHeading);
    
    if (!_longitude) {
        _latitude = [NSString stringWithFormat:@"%f",userLocation.coordinate.latitude];
        _longitude = [NSString stringWithFormat:@"%f",userLocation.coordinate.longitude];
    }
    
    if (!_point) {
        _point = [[QPointAnnotation alloc] init];
        
        _point.title = @"长按拖动图标选择位置";
        CLLocationDegrees latitude = _mapView.centerCoordinate.latitude;
        
        CLLocationDegrees longitude = _mapView.centerCoordinate.longitude;
        
        
        _point.coordinate = CLLocationCoordinate2DMake(latitude
                                                       ,longitude);
        //向mapview添加annotation
        [_mapView addAnnotation:_point];
        [_mapView selectAnnotation:_point animated:YES];

    }
}


- (void)mapView:(QMapView *)mapView annotationView:(QAnnotationView *)view didChangeDragState:(QAnnotationViewDragState)newState fromOldState:(QAnnotationViewDragState)oldState{
    NSLog(@"移动了 位置为- > %f,%f,",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);
    _latitude = [NSString stringWithFormat:@"%f",view.annotation.coordinate.latitude];
    _longitude = [NSString stringWithFormat:@"%f",view.annotation.coordinate.longitude];
}

-(QAnnotationView *)mapView:(QMapView *)mapView
          viewForAnnotation:(id<QAnnotation>)annotation {
    static NSString *pointReuseIndentifier = @"pointReuseIdentifier";
    
    if ([annotation isKindOfClass:[QPointAnnotation class]]) {
        //添加默认pinAnnotation
        
        QPinAnnotationView *annotationView = (QPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[QPinAnnotationView alloc]
                              initWithAnnotation:annotation
                              reuseIdentifier:pointReuseIndentifier];
        }
        //显示气泡，默认NO
        annotationView.canShowCallout = YES;
        
        
        //设置大头针颜色
        annotationView.pinColor = QPinAnnotationColorGreen;

        //可以拖动
        annotationView.draggable = YES;
        
        
        return annotationView;
    }
    return nil;
}

- (void)mapViewWillStartLocatingUser:(QMapView *)mapView
{
    //获取开始定位的状态
}

- (void)mapViewDidStopLocatingUser:(QMapView *)mapView
{
    //获取停止定位的状态
}


- (IBAction)backButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

//保存
- (IBAction)saveButtonClick:(UIButton *)sender {
//    NSString *string= [NSString stringWithFormat:@"%@,%@",_latitude,_longitude];
////    kAlert(string);
//    NSDictionary *dict = @{
//                           @"indexPath" : _indexPath,
//                           @"name" : string
//                           };
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"place" object:dict];
    
    [_delegate getShoplLatitude:_latitude longitude:_longitude];
    
    [self backButtonClick:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
